# 【一】导入模块
# 【1】谷歌浏览器对象
import time

from selenium.webdriver import Chrome
# 【2】驱动位置服务
from selenium.webdriver.chrome.service import Service
# 【3】选择器
from selenium.webdriver.common.by import By
# 【4】动作链
from selenium.webdriver import ActionChains

# 加载浏览器驱动
executable_path = "./chromedriver.exe"
service = Service(executable_path=executable_path)
browser = Chrome(service=service)

# 打开网址
browser.get("http://sahitest.com/demo/dragDropMooTools.htm")
time.sleep(3)

# 拖拽任务
original_ele = browser.find_element(By.CSS_SELECTOR, "#dragger")
ele_target1 = browser.find_element(By.CSS_SELECTOR, "body > div:nth-child(4)")
ele_target2 = browser.find_element(By.CSS_SELECTOR, "body > div:nth-child(5)")
ele_target3 = browser.find_element(By.CSS_SELECTOR, "body > div:nth-child(6)")
ele_target4 = browser.find_element(By.CSS_SELECTOR, "body > div:nth-child(7)")
# (1) 根据元素拖拽
"""
# 任务一：
action = ActionChains(browser)
action.drag_and_drop(original_ele, ele_target1).perform()
time.sleep(3)
# 任务二：
action.drag_and_drop(original_ele, ele_target2).perform()
time.sleep(3)
# 任务三：
action.drag_and_drop(original_ele, ele_target3).perform()
time.sleep(3)
# 任务四：
action.drag_and_drop(original_ele, ele_target4).perform()
time.sleep(3)
"""

# (2) 根据坐标拖拽
actions = ActionChains(browser)
# actions.drag_and_drop_by_offset(source=original_ele, xoffset=40, yoffset=100).perform()
# time.sleep(1)
# actions.drag_and_drop_by_offset(source=original_ele, xoffset=80, yoffset=100).perform()
# time.sleep(1)
# actions.drag_and_drop_by_offset(source=original_ele, xoffset=120, yoffset=100).perform()
# time.sleep(1)
# actions.drag_and_drop_by_offset(source=original_ele, xoffset=160, yoffset=100).perform()

# (3) 先按住然后到指定为止再 松开
# 重新进入完整
time.sleep(4)
actions.click_and_hold(original_ele).move_to_element(ele_target1).release().perform()
time.sleep(1)
actions.click_and_hold(original_ele).move_to_element(ele_target2).release().perform()
time.sleep(1)
actions.click_and_hold(original_ele).move_to_element(ele_target3).release().perform()
time.sleep(1)
actions.click_and_hold(original_ele).move_to_element(ele_target4).release().perform()
time.sleep(1)
